#pragma once
#include<iostream>
#include<functional>
#include<string>
#include<unistd.h>
#define ListenConnection 1
#define NormalConnection 2
class Reactor;
class Connection;
using handler_t = std::function<void(Connection*)>;
class Connection
{
public:
    Connection(int sockfd):_sockfd(sockfd)
    {
    };
    void SetEvents(uint32_t event)
    {_events = event;}
    void SetHandler(handler_t handler_recver,handler_t handler_sender,handler_t handler_excepter)
    {
        _handler_recver=handler_recver;
        _handler_sender=handler_sender;
        _handler_excepter=handler_excepter;
    }
    void SetReactor(Reactor* R)
    {_R=R;}
    void SetType(int type)
    {_type=type;}
    int Type()
    {return _type;}
    int Sockfd()
    {return _sockfd;}
    uint32_t Events()
    {return _events;}
    std::string& InBuffer()
    {return _in_buffer;}
    std::string& OutBuffer()
    {return _out_buffer;} 
    void AddInBuffer(const std::string& in)
    {_in_buffer+=in;}
    void AddOutBuffer(const std::string& in)
    {_out_buffer+=in;}
    void DelOutbuffer(int n)
    {_out_buffer.erase(0,n);}
    void Close()
    {
        if(_sockfd>=0)
        close(_sockfd);
    }

private:
    int _sockfd;
    uint32_t _events;
    std::string _in_buffer;
    std::string _out_buffer;
    int _type;
public:
    handler_t _handler_recver;
    handler_t _handler_sender;
    handler_t _handler_excepter;
    Reactor* _R;
};